package com.igeek.health.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.health.entity.Checkgroup;
import com.igeek.health.entity.CheckgroupCheckitem;
import com.igeek.health.entity.Checkitem;
import com.igeek.health.entity.SetmealCheckgroup;
import com.igeek.health.exception.HealthException;
import com.igeek.health.mapper.CheckgroupCheckitemMapper;
import com.igeek.health.mapper.CheckgroupMapper;
import com.igeek.health.mapper.SetmealCheckgroupMapper;
import com.igeek.health.service.ICheckgroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.health.vo.PageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fengqq
 * @since 2024-08-26
 */
@Service
public class CheckgroupServiceImpl extends ServiceImpl<CheckgroupMapper, Checkgroup> implements ICheckgroupService {

    @Autowired
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;
    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;
    //添加检查组信息
    @Transactional
    @Override
    public boolean add(Checkgroup checkgroup, Integer[] checkitemIds) {
        //1.添加检查组信息 添加成功后会自行封装主键
        int row1 = baseMapper.insert(checkgroup);
        //2.给中间表 插入数据 检查项&检查组
        insertCheckgroupCheckitem(checkgroup,checkitemIds);
        return row1>0;
    }

    //给中间表 插入数据 检查项&检查组
    private void insertCheckgroupCheckitem(Checkgroup checkgroup, Integer[] checkitemIds){
        Stream.of(checkitemIds).forEach(checkitemId->{
            CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem();
            checkgroupCheckitem.setCheckitemId(checkitemId);
            checkgroupCheckitem.setCheckgroupId(checkgroup.getId());
            int row = checkgroupCheckitemMapper.insert(checkgroupCheckitem);
        });
    }

    @Transactional(readOnly = true)
    @Override
    public Page<Checkgroup> findPage(PageQueryVO vo) {
        if(ObjectUtil.isEmpty(vo)){
            return null;
        }
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();

        //封装page对象
        Page<Checkgroup> page = new Page<>(currentPage, pageSize);
        //封装条件
        LambdaQueryWrapper<Checkgroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.or().eq(StringUtils.hasLength(queryString),Checkgroup::getCode,queryString);
        queryWrapper.or().like(StringUtils.hasLength(queryString),Checkgroup::getName,queryString);

        //执行分页查询
        Page<Checkgroup> resultPage = this.baseMapper.selectPage(page, queryWrapper);
        return resultPage;
    }

    //根据检查组编号，查询其包含的检查项信息
    @Transactional(readOnly = true)
    @Override
    public List<Integer> findCheckitemsByCheckgroupId(Integer id) {
        List<CheckgroupCheckitem> checkitemList = checkgroupCheckitemMapper.selectList(
                new LambdaQueryWrapper<CheckgroupCheckitem>().eq(CheckgroupCheckitem::getCheckgroupId, id));
        //List<CheckgroupCheckitem> --> List<Integer>
        List<Integer> checkitemIds =
                checkitemList.stream().map(CheckgroupCheckitem::getCheckitemId).collect(Collectors.toList());
        return checkitemIds;
    }

    //根据检查组id编辑检查组信息
    @Transactional
    @Override
    public boolean update(Integer[] checkitemIds, Checkgroup checkgroup) {
        //1.根据检查组id 先删除中间表原来的数据
        int row = checkgroupCheckitemMapper.delete(
                new LambdaQueryWrapper<CheckgroupCheckitem>()
                        .eq(CheckgroupCheckitem::getCheckgroupId, checkgroup.getId()));
        //2.更新检查组信息
        baseMapper.updateById(checkgroup);
//        int i = 1/0; 引发异常
        //3.根据检查组 和 勾选的检查项id  插入中间表数据
        insertCheckgroupCheckitem(checkgroup,checkitemIds);
        return row>0;
    }

    //根据检查组id删除信息
    @Transactional
    @Override
    public boolean delete(Integer id) {
        //1.在setmeal_checkgroup表中 查询是否有当前检查组id的数据  若有 则不能删除
        List<SetmealCheckgroup> selectList = setmealCheckgroupMapper.selectList(
                new LambdaQueryWrapper<SetmealCheckgroup>()
                        .eq(SetmealCheckgroup::getCheckgroupId, id));
        if(CollectionUtil.isNotEmpty(selectList)){
            throw new HealthException("当前检查组正在使用中，删除失败");
        }

        //2.在setmeal_checkgroup表中 查询是否有当前检查组id的数据  若没有 则可以删除
        //2.1.在checkgroup_checkitem表中 根据当前检查组id 删除数据
        int row = checkgroupCheckitemMapper.delete(
                new LambdaQueryWrapper<CheckgroupCheckitem>()
                        .eq(CheckgroupCheckitem::getCheckgroupId, id));
        int i = 1/0;
        //2.2.在checkgroup表中 根据当前检查组id 删除数据
        int row2 = baseMapper.deleteById(id);
        return row>0&&row2>0;
    }

    //查询所有检查组信息
    @Transactional(readOnly = true)
    @Override
    public List<Checkgroup> findAll() {
        List<Checkgroup> checkgroupList = baseMapper.selectList(null);
        return checkgroupList;
    }
}
